package org.example.composite;

import java.util.ArrayList;
import java.util.List;


//组合模式（Composite）是一种结构型设计模式，它允许你将对象组合成树形结构以表示“部分-整体”的层次结构。
// 组合模式使得用户对单个对象和组合对象的使用具有一致性。


// 1. 组件接口 - 定义文件系统组件的通用操作
interface FileSystemComponent {
    void display(int depth); // 显示组件信息

    long getSize(); // 获取组件大小

    void add(FileSystemComponent component); // 添加子组件

    void remove(FileSystemComponent component); // 移除子组件
}

// 2. 叶子组件 - 文件
class File implements FileSystemComponent {
    private String name;
    private long size; // 文件大小（字节）

    public File(String name, long size) {
        this.name = name;
        this.size = size;
    }

    @Override
    public void display(int depth) {
        System.out.println(" ".repeat(depth * 2) + "📄 " + name + " (" + size + " bytes)");
    }

    @Override
    public long getSize() {
        return size;
    }

    @Override
    public void add(FileSystemComponent component) {
        throw new UnsupportedOperationException("文件不支持添加子组件");
    }

    @Override
    public void remove(FileSystemComponent component) {
        throw new UnsupportedOperationException("文件不支持移除子组件");
    }
}

// 3. 复合组件 - 文件夹
class Directory implements FileSystemComponent {
    private String name;
    private List<FileSystemComponent> children = new ArrayList<>();

    public Directory(String name) {
        this.name = name;
    }

    @Override
    public void display(int depth) {
        System.out.println(" ".repeat(depth * 2) + "📁 " + name + " (" + getSize() + " bytes)");
        for (FileSystemComponent child : children) {
            child.display(depth + 1);
        }
    }

    @Override
    public long getSize() {
        long totalSize = 0;
        for (FileSystemComponent child : children) {
            totalSize += child.getSize();
        }
        return totalSize;
    }

    @Override
    public void add(FileSystemComponent component) {
        children.add(component);
    }

    @Override
    public void remove(FileSystemComponent component) {
        children.remove(component);
    }
}

// 4. 客户端代码
public class CompositeDemo {
    public static void main(String[] args) {
        // 创建文件
        FileSystemComponent file1 = new File("report.docx", 1024);
        FileSystemComponent file2 = new File("presentation.pptx", 2048);
        FileSystemComponent file3 = new File("budget.xlsx", 512);
        FileSystemComponent file4 = new File("image.jpg", 3072);
        FileSystemComponent file5 = new File("notes.txt", 256);

        // 创建文件夹
        Directory documents = new Directory("Documents");
        Directory work = new Directory("Work");
        Directory personal = new Directory("Personal");
        Directory pictures = new Directory("Pictures");
        Directory root = new Directory("Root");

        // 构建树形结构
        work.add(file1);
        work.add(file2);
        work.add(file3);

        personal.add(file5);

        pictures.add(file4);

        documents.add(work);
        documents.add(personal);

        root.add(documents);
        root.add(pictures);

        // 显示文件系统结构
        System.out.println("文件系统结构:");
        root.display(0);

        // 计算总大小
        System.out.println("\n总大小: " + root.getSize() + " bytes");

        // 添加新文件
        System.out.println("\n添加新文件后:");
        personal.add(new File("resume.pdf", 768));
        root.display(0);
        System.out.println("新总大小: " + root.getSize() + " bytes");

        // 移除文件夹
        System.out.println("\n移除Pictures文件夹后:");
        root.remove(pictures);
        root.display(0);
        System.out.println("新总大小: " + root.getSize() + " bytes");
    }
}